//
//  AllRiceView.swift
//  LeGou
//
//  Created by lxm on 2022/7/28.
//

import UIKit

class AllRiceView: UIView {

    //MARK: --------------------属性--------------------
    // 0 left 1 right
    var onClick : ((_ type : Int)->())?
    //MARK: --------------------UI控件--------------------
    let title = ZMUtil.label(title: "解锁共富收益", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 14, weight: .medium))
    
    let doTask = ZMUtil.label(title: "做任务", color: "#DE3F26".zm.hex(), font: UIFont.systemFont(ofSize: 12, weight: .regular))
    
    let rightIcon = ZMUtil.imageView(imageName: "right_red")
    
    let richIcon = ZMUtil.imageView(imageName: "mine_rich_icon")

    let leftContentView = UIView().then({$0.layer.cornerRadius = 12;$0.layer.masksToBounds = true;$0.backgroundColor = .white})
    
    let rightContentView = UIView().then({$0.layer.cornerRadius = 12;$0.layer.masksToBounds = true;$0.backgroundColor = .white})
    
    let contentStack = ZMUtil.stackView(axis: .horizontal, spacing: 9).then { s in
        s.distribution = .fillEqually
    }
    
    let totalActivity = ZMUtil.label(title: "16", color: "#333333".zm.hex(), font: Style.FONT_Open_Sans_SemiBold(16))
    
    let totalActivityDes = ZMUtil.label(title: "团队活跃度", color: "#333333".zm.hex(), font: UIFont.systemFont(ofSize: 12))
    
    let todayActivity = ZMUtil.label(title: "16", color: "#333333".zm.hex(), font: Style.FONT_Open_Sans_SemiBold(16))

    let todayActivityDes = ZMUtil.label(title: "个人活跃度", color: "#333333".zm.hex(), font: UIFont.systemFont(ofSize: 12))

    let rightStack = ZMUtil.stackView(axis: .horizontal, spacing: 16).then({$0.alignment = .center; $0.distribution = .fill})
    
    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        bindAction()
        addConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        bindAction()
        addConstraints()
    }

    //MARK: --------------------UI--------------------
    func setUI() {
        contentStack.addArrangedSubview(leftContentView)
        contentStack.addArrangedSubview(rightContentView)
        
        leftContentView.addSubview(title)
        
        leftContentView.addSubview(doTask)
        
        leftContentView.addSubview(rightIcon)
        
        leftContentView.addSubview(richIcon)

        
        let stackOne = ZMUtil.stackView(axis: .vertical, spacing: 2)
        stackOne.alignment = .center
        stackOne.addArrangedSubview(totalActivity)
        stackOne.addArrangedSubview(totalActivityDes)
        rightStack.addArrangedSubview(stackOne)

        let line = UIView().then { v in
            v.snp.makeConstraints { make in
                make.width.equalTo(0.5)
                make.height.equalTo(17)
            }
            v.backgroundColor = Style.lineColor
        }
        rightStack.addArrangedSubview(line)
        
        let stackTwo = ZMUtil.stackView(axis: .vertical, spacing: 2)
        stackTwo.alignment = .center
        stackTwo.addArrangedSubview(todayActivity)
        stackTwo.addArrangedSubview(todayActivityDes)
        rightStack.addArrangedSubview(line)
        rightStack.addArrangedSubview(stackTwo)

        rightContentView.addSubview(rightStack)
        
        let leftTap = UITapGestureRecognizer()
        leftTap.addTarget(self, action: #selector(onLeftTap))
        leftContentView.addGestureRecognizer(leftTap)
        
        let rightTap = UITapGestureRecognizer()
        rightTap.addTarget(self, action: #selector(onRightTap))
        rightContentView.addGestureRecognizer(rightTap)
        addSubview(contentStack)
    }
    
    func addConstraints() {
        contentStack.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        title.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(12)
        }
        
        doTask.snp.makeConstraints { make in
            make.top.equalTo(title.snp.bottom).offset(8)
            make.left.equalToSuperview().offset(12)
        }
        
        rightIcon.snp.makeConstraints { make in
            make.centerY.equalTo(doTask)
            make.left.equalTo(doTask.snp.right).offset(4)
        }
        
        richIcon.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview()
            make.width.equalTo(52)
            make.height.equalTo(50)
        }
        
        rightStack.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.center.equalToSuperview()
            make.left.greaterThanOrEqualToSuperview()
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        
    }
    
    @objc func onRightTap() {
        if let onClick = onClick {
            onClick(1)
        }
    }
    
    @objc func onLeftTap() {
        if let onClick = onClick {
            onClick(0)
        }
    }
    
    
    //MARK: --------------------delegate--------------------

}
